﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Framework.Serializer
{
    public static class ExtensionXml
    {
        static XmlSerializer serializer = new XmlSerializer();

        #region 序列化
        /// <summary>
        /// 序列化成String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String SToStringXml(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileXml(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromStringXml<T>(this String obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileXml<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion
    }

    public static class ExtensionJavaScript
    {
        static JavaScriptSerializer serializer = new JavaScriptSerializer();

        #region 序列化
        /// <summary>
        /// 序列化成String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String SToStringJavaScript(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileJavaScript(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromStringJavaScript<T>(this String obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileJavaScript<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion

    }

    public static class ExtensionDataContract
    {
        static DataContractSerializer serializer = new DataContractSerializer();

        #region 序列化
        /// <summary>
        /// 序列化为String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String SToStringDataContract(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileDataContract(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromStringDataContract<T>(this String obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileDataContract<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion

    }

    public static class ExtensionBinary
    {
        static BinarySerializer serializer = new BinarySerializer();
        #region 序列化
        /// <summary>
        /// 序列化为Byte[]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Byte[] SToBytesBinary(this Object obj)
        {
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 序列化到文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SToFileBinary(this Object obj, string filePath)
        {
            return serializer.SerializeToFile(obj, filePath);
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 反序列化为 T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SFromBytesBinary<T>(this Byte[] obj)
        {
            return serializer.DeSerialize<T>(obj);
        }
        /// <summary>
        /// 从文件反序列化为 T 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T SFromFileBinary<T>(string filePath)
        {
            return serializer.DeSerializeFromFile<T>(filePath);
        }
        #endregion
    }

}
